package com.youzhi.backend.service.impl;

import cn.hutool.json.JSONUtil;
import com.youzhi.backend.config.KafkaConfig;
import com.youzhi.backend.dto.Message;
import com.youzhi.backend.dto.MessageType;
import com.youzhi.backend.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {


    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Override
    public void sendMessage(MessageType type, Object data) {
        Message message = new Message();
        message.setType(type.type);
        message.setDesc(type.desc);
        message.setData(data);
        String messageStr = JSONUtil.toJsonStr(message);
        log.info("发送kafka消息:{}", messageStr);
        kafkaTemplate.send(KafkaConfig.topic_message, message.getType(), messageStr);
    }


}
